/* 
 * Copyright 2011-2012 Andrew Brock
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.catchwa.myvideobrowser.gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import org.catchwa.myvideobrowser.Main;
import org.catchwa.myvideobrowser.model.Movie;

/**
 *
 * @author Andrew Brock
 */
public class MovieTableModel extends AbstractTableModel {

  protected int sortColumnIndex;
  protected boolean sortAscending;
  private List<Movie> movies = new ArrayList<Movie>();
  public static String[] columns = {"Title", "Released", "Runtime", "TMDb Rating", "Watched"};

  public MovieTableModel() {
  }

  public void add(Movie movie) {
    movies.add(movie);
    fireTableRowsInserted(movies.indexOf(movie), movies.indexOf(movie));
  }

  public void remove(Movie movie) {
    int index = movies.indexOf(movie);
    movies.remove(movie);
    fireTableRowsDeleted(index, index);
  }

  public void removeAll() {
    int size = movies.size();
    if (size > 0) {
      while (movies.size() > 0) {
        movies.remove(0);
      }
      fireTableRowsDeleted(0, size - 1);
    }
  }

  public Movie getMovie(int index) {
    return movies.get(index);
  }

  @Override
  public boolean isCellEditable(int row, int col) {
    if(col == 4) {
      return true;
    }
    return false;
  }

  @Override
  public int getRowCount() {
    return movies.size();
  }

  @Override
  public int getColumnCount() {
    return columns.length;
  }

  @Override
  public String getColumnName(int index) {
    return columns[index];
  }

  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    if (columns[columnIndex].equals("Title")) {
      return movies.get(rowIndex).getTitle();
    }
    if (columns[columnIndex].equals("Released")) {
      return movies.get(rowIndex).getReleased();
    }
    if (columns[columnIndex].equals("Runtime")) {
      return movies.get(rowIndex).getRuntime();
    }
    if (columns[columnIndex].equals("TMDb Rating")) {
      return movies.get(rowIndex).getTmdbRating();
    }
    if (columns[columnIndex].equals("Watched")) {
      return movies.get(rowIndex).getWatched();
    }
    return null;
  }
  
  @Override
  public void setValueAt(Object value, int rowIndex, int columnIndex) {
    if (columnIndex == 4) {
      Boolean watched = (Boolean) value;
      movies.get(rowIndex).setWatched(watched.booleanValue());
      Main.getDatabase().toggleWatched(movies.get(rowIndex));
    }
  }
  
  @Override
  public Class getColumnClass(int columnIndex) {
        if (columns[columnIndex].equals("Title")) {
      return String.class;
    }
    if (columns[columnIndex].equals("Released")) {
      return String.class;
    }
    if (columns[columnIndex].equals("Runtime")) {
      return Integer.class;
    }
    if (columns[columnIndex].equals("TMDb Rating")) {
      return Double.class;
    }
    if (columns[columnIndex].equals("Watched")) {
      return Boolean.class;
    }
    return null;
  }
}
